package com.atguigu.auth.activiti;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class ProcessTestDemo3 {
    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;

    //部署流程定义
    @Test
    void deployProcess(){
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/jiaban04.bpmn20.xml")
                .name("加班流程4")
                .deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }

    //创建流程实例
    @Test
    void startProcessInstance(){
        ProcessInstance instance = runtimeService.startProcessInstanceByKey("jiaban04");
        System.out.println(instance.getProcessDefinitionId());
        System.out.println(instance.getId());
    }

    //查询组任务
    @Test
    void findGroupTaskList(){
        //查询候选人tom01的所有待办任务
        List<Task> list = taskService.createTaskQuery()
                .taskCandidateUser("tom01")
                .list();
        for(Task task : list){
            System.out.println(task.getProcessInstanceId());
            System.out.println(task.getId());
            System.out.println(task.getAssignee());
            System.out.println(task.getName());
        }
    }

    //让其中一个候选人拾取任务
    @Test
    void claimTask(){
        Task task = taskService.createTaskQuery()
                .taskCandidateUser("tom01")
                .singleResult();
        if(task != null){
            taskService.claim(task.getId(),"tom01");
            System.out.println("tom01拾取了一个任务");
        }
    }

    //查询某人的待办任务
    @Test
    void findTask(){
        List<Task> list = taskService.createTaskQuery()
                .taskAssignee("tom01")
                .list();
        for(Task task : list){
            System.out.println(task.getProcessInstanceId());
            System.out.println(task.getId());
            System.out.println(task.getAssignee());
            System.out.println(task.getName());
        }
    }

    //办理个人任务
    @Test
    void complete(){
        Task task = taskService.createTaskQuery()
                .taskAssignee("tom01")
                .singleResult();
        taskService.complete(task.getId());
    }
}
